package net.seninp.fdata.data;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.hackystat.utilities.stacktrace.StackTrace;

public class FDataFactory {

  public static Measurements getMeasuremenets(String inname) throws IOException {
    Measurements res = new Measurements();
    BufferedReader in = new BufferedReader(new FileReader(inname));
    String line = null;
    // skip the first line
    line = in.readLine();
    while ((line = in.readLine()) != null) {
      Measurement m = null;
      try {
        m = new Measurement(line);
      }
      catch (Exception e) {
        System.err.println("Exception thrown while parsing the line '" + line
            + "' into the Measurement object.\n Exception trace:\n" + StackTrace.toString(e));
        continue;
      }
      res.add(m);
    }
    return res;
  }

  public static MeasurementsGrid mapToGridByWindow(Measurements measurements,
      Integer gridLatdSsize, Integer gridLondSize) {
    MeasurementsGrid res = new MeasurementsGrid(measurements.getMinLatd(), measurements
        .getMaxLatd(), measurements.getMinLond(), measurements.getMaxLond(), gridLatdSsize,
        gridLondSize);
    for (Measurement m : measurements) {
      Cell cell = res.put(m);
      if (null == cell) {
        System.out.println("Oops!");
      }
    }
    return res;
  }
}
